


#include "Debug.h"
#include "Log.h"
#include "BSP_Sleep.h"

#include <Reader/Reader.h>

#undef FAIL

#include "unity.h"
#include "unity_fixture.h"

#define  DEBUG_PRINTF(...)	LOG(__VA_ARGS__)
#define PTHRU_ON_OFF            (1 << 6)   /* NC_REG Pass-through bit */

TEST_GROUP(Ultralight);


TEST_SETUP(Ultralight)
{
	reader.hfFlags = TAG_MASK_MIFARE;
	Reader_NFC_LoadProfile(reader.hfFlags, 0, 0, 0);
}

TEST_TEAR_DOWN(Ultralight)
{
	Reader_NFC_RfOff();
}


IGNORE_TEST(Ultralight, GetVersion)
{
    phStatus_t  status = 0;
	int ret;
	int i;
	SimpleTag_T simpleTag;
	phalMful_Sw_DataParams_t		   * palMful;		/* Pointer to AL MFUL data-params */
	palMful = (phalMful_Sw_DataParams_t *) phNfcLib_GetDataParams(PH_COMP_AL_MFUL);
	uint8_t 	bDataBuffer[256];  /* universal data buffer */
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(ret){
			continue;
		}
		status = phalMful_GetVersion(palMful, &bDataBuffer[0]);
		if(status){
			TEST_PRINTF("status=%X", status);
			FAIL("phalMful_GetVersion error");
			continue;
		}
		LOG_BUFS("Vsersion", bDataBuffer, 7);
		/* Check for Tag Info */
		if((bDataBuffer[0] == 0x00) && (bDataBuffer[1] == 0x04) && (bDataBuffer[2] == 0x03))
		{
			LOG_I ("\nProduct: MIFARE Ultralight ");
			if(bDataBuffer[4] == 0x04)
			{
				LOG_I ("AES Version ");
			}
			else
			{
				LOG_E ("\nInvalid Tag Version");
				LOG_E("\n\nExecution aborted!!!\n");
			}
		}
		break;
	}
}

IGNORE_TEST(Ultralight, ReadWrite)
{
    phStatus_t  status = 0;
	int ret;
	int i;
	SimpleTag_T simpleTag;
	phalMful_Sw_DataParams_t* palMful;		/* Pointer to AL MFUL data-params */
	palMful = (phalMful_Sw_DataParams_t *) phNfcLib_GetDataParams(PH_COMP_AL_MFUL);
	uint8_t bDataBuffer[256];  /* universal data buffer */
    uint8_t     *pBuff;
    uint16_t    numOfBytes;
    uint8_t     bSectorNum;
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(ret){
			continue;
		}

		break;
	}
	
	status = phalMful_GetVersion(palMful, &bDataBuffer[0]);
	if(status){
		TEST_PRINTF("status=%X", status);
		FAIL("phalMful_GetVersion fail");
	}
	LOG_BUFS("Vsersion", bDataBuffer, 7);
	/* Check for Tag Info */
	if((bDataBuffer[0] == 0x00) && (bDataBuffer[1] == 0x04) && (bDataBuffer[2] == 0x03))
	{
		DEBUG_PRINTF ("\nProduct: MIFARE Ultralight ");
		if(bDataBuffer[4] == 0x04)
		{
			DEBUG_PRINTF ("AES Version \n\n");
		}
		else
		{
			LOG_E ("Invalid Tag Version");
			LOG_E("Execution aborted!!!");
			FAIL("phalMful_GetVersion error");
		}
	}
	
	/* Read Tag Mode */
	
	/* Read Session registers */
	status = phalMful_Read(palMful, 7, bDataBuffer);
	
	/* check for Error */
	if ((status & PH_ERR_MASK) != PH_ERR_SUCCESS)
	{
		LOG_E("\nSession Registers Read Failed");
		LOG_E("\nUnable to Read Tag Mode");
		LOG_E("\n\nExecution aborted!!!\n");
		FAIL("phalMful_Read fail");
	}
	
	/* check for Pass-through mode */
	if ((bDataBuffer[0] & PTHRU_ON_OFF))
	{
		DEBUG_PRINTF ("\nTag is Configured in Pass-through Mode");
		DEBUG_PRINTF ("\nCan not perform Sector/Page Read Write Operation");
		DEBUG_PRINTF("\n\nExecution aborted!!!\n");
		FAIL("phalMful_Read fail");
	}
	
	/* --- Read Operation --- */
	/* Read Sector 0 Page 5 to 8 Data */
	status = phalMful_FastRead(palMful, 5, 8, &pBuff, &numOfBytes);
	
	/* check for Success */
	if ((status & PH_ERR_MASK) == PH_ERR_SUCCESS)
	{
		DEBUG_PRINTF ("\n\nRead Success");
	
		/* Copy Sector0 Page 5 to 6 Data */
		memcpy(bDataBuffer, pBuff, numOfBytes);
	
		/* Display contents of Sector 0 Pages 5 to 6 Data */
		for (bSectorNum = 5; bSectorNum <= 8; bSectorNum++)
		{
			DEBUG_PRINTF ("\nSector 0 Page %d Data: ", bSectorNum);
			phApp_Print_Buff(&bDataBuffer[0 + ((bSectorNum - 5) * 4)], 4);
		}
	}
	
	/* Check for Error */
	if ((status & PH_ERR_MASK) != PH_ERR_SUCCESS)
	{
		DEBUG_PRINTF ("\nRead Operation Failed!!!");
		DEBUG_PRINTF("\n\nExecution aborted!!!\n");
		FAIL("phalMful_Read fail");
	}
	
	/* End of Read Operation */
	DEBUG_PRINTF("\n\n --- End of Read Operation --- \n");
	/* Write data to Sector 0 Page 5 to 8 */
	DEBUG_PRINTF("\nWrite operation to write back the Sector 0 Pages 5 to 8 Data \n");
	for(uint8_t i = 0; i < 32; i++){
		bDataBuffer[i] += i;
	}
	DEBUG_PRINTF ("\nData: ");
	phApp_Print_Buff(&bDataBuffer[0], 32);
	for (bSectorNum = 5; bSectorNum <= 8; bSectorNum++)
	{
		/* Page Write */
		status = phalMful_Write(palMful, bSectorNum, &bDataBuffer[0 + ((bSectorNum - 5) * 4)]);
	
		/* Check for Success */
		if ((status & PH_ERR_MASK) == PH_ERR_SUCCESS)
		{
			DEBUG_PRINTF("\nWrite Success: Sector 0 Page %d", bSectorNum);
		}
		else
		{
			DEBUG_PRINTF ("\nWrite Failed: Sector 0 Page %d ", bSectorNum);
			break;
		}
	}

	
	/* Read Sector 0 Page 5 to 8 Data */
	status = phalMful_FastRead(palMful, 5, 8, &pBuff, &numOfBytes);
	
	/* check for Success */
	if ((status & PH_ERR_MASK) == PH_ERR_SUCCESS)
	{
		DEBUG_PRINTF ("\n\nRead Success");
	
		/* Copy Sector0 Page 5 to 6 Data */
		memcpy(bDataBuffer, pBuff, numOfBytes);
	
		/* Display contents of Sector 0 Pages 5 to 6 Data */
		for (bSectorNum = 5; bSectorNum <= 8; bSectorNum++)
		{
			DEBUG_PRINTF ("\nSector 0 Page %d Data: ", bSectorNum);
			phApp_Print_Buff(&bDataBuffer[0 + ((bSectorNum - 5) * 4)], 4);
		}
	}
}


IGNORE_TEST(Ultralight, Read2)
{
    phStatus_t  status = 0;
	int ret;
	int i;
	SimpleTag_T simpleTag;
	phalMful_Sw_DataParams_t* palMful;		/* Pointer to AL MFUL data-params */
	palMful = (phalMful_Sw_DataParams_t *) phNfcLib_GetDataParams(PH_COMP_AL_MFUL);
	uint8_t bDataBuffer[256];  /* universal data buffer */
    uint8_t     *pBuff;
    uint16_t    numOfBytes;
    uint8_t     bSectorNum;
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(!ret){
			break;
		}
	}
	
	status = phalMful_GetVersion(palMful, &bDataBuffer[0]);
	if(status){
		TEST_PRINTF("status=%X", status);
		FAIL("phalMful_GetVersion fail");
	}
	LOG_BUFS("Vsersion", bDataBuffer, 7);
	/* Check for Tag Info */
	if((bDataBuffer[0] == 0x00) && (bDataBuffer[1] == 0x04) && (bDataBuffer[2] == 0x03))
	{
		DEBUG_PRINTF ("\nProduct: MIFARE Ultralight ");
		if(bDataBuffer[4] == 0x04)
		{
			DEBUG_PRINTF ("AES Version \n\n");
		}
		else
		{
			LOG_E ("Invalid Tag Version");
			LOG_E("Execution aborted!!!");
			FAIL("phalMful_GetVersion error");
		}
	}
	
	/* Read Tag Mode */
	
	/* Read Session registers */
	status = phalMful_ReadCnt(palMful, 0, bDataBuffer);
	
	/* check for Error */
	if ((status & PH_ERR_MASK) != PH_ERR_SUCCESS)
	{
		LOG_E("\nphalMful_ReadCnt Failed");
		LOG_E("\n\nExecution aborted!!!\n");
		FAIL("phalMful_ReadCnt fail");
	}
	LOG_BUFS("ReadCnt", bDataBuffer, 3);
	uint8_t* pSign;
	uint16_t signLen;
	status = phalMful_ReadSign(palMful, 0x00, &pSign, &signLen);
	if ((status & PH_ERR_MASK) != PH_ERR_SUCCESS)
	{
		LOG_E("\nphalMful_ReadSign Failed");
		LOG_E("\n\nExecution aborted!!!\n");
		FAIL("phalMful_ReadSign fail");
	}
	LOG_BUFS("Sign", pSign, signLen);
}

TEST(Ultralight, Auth)
{
    phStatus_t  status = 0;
	int ret;
	int i;
	SimpleTag_T simpleTag;
	phalMful_Sw_DataParams_t* palMful;		/* Pointer to AL MFUL data-params */
	palMful = (phalMful_Sw_DataParams_t *) phNfcLib_GetDataParams(PH_COMP_AL_MFUL);
	uint8_t bDataBuffer[256];  /* universal data buffer */
    uint8_t     *pBuff;
    uint16_t    numOfBytes;
    uint8_t     bSectorNum;
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(!ret){
			break;
		}
	}
	
	status = phalMful_GetVersion(palMful, &bDataBuffer[0]);
	if(status){
		TEST_PRINTF("status=%X", status);
		FAIL("phalMful_GetVersion fail");
	}
	LOG_BUFS("Vsersion", bDataBuffer, 7);
	/* Check for Tag Info */
	if((bDataBuffer[0] == 0x00) && (bDataBuffer[1] == 0x04) && (bDataBuffer[2] == 0x03))
	{
		DEBUG_PRINTF ("\nProduct: MIFARE Ultralight ");
		if(bDataBuffer[4] == 0x04)
		{
			DEBUG_PRINTF ("AES Version \n\n");
		}
		else
		{
			LOG_E ("Invalid Tag Version");
			LOG_E("Execution aborted!!!");
			FAIL("phalMful_GetVersion error");
		}
	}
	DEBUG_PRINTF ("\n\nRead Key 0");
	status = phalMful_FastRead(palMful, 0x30, 0x33, &pBuff, &numOfBytes);
	
	/* check for Success */
	if ((status & PH_ERR_MASK) == PH_ERR_SUCCESS)
	{
		DEBUG_PRINTF ("\n\nKey 0\n");
	
		phApp_Print_Buff(pBuff, numOfBytes);
	}
	else{
		FAIL("Read Key 0 Fail");
	}
	void *psKeyStore = phNfcLib_GetDataParams(PH_COMP_KEYSTORE);
	
	phKeyStore_FormatKeyEntry(psKeyStore, 1, PH_KEYSTORE_KEY_TYPE_AES128);
	phKeyStore_SetKey(psKeyStore, 1, 0,
							 PH_KEYSTORE_KEY_TYPE_AES128, pBuff, 0);
	status = phalMful_Sw_AuthenticateAES(palMful, 1, 0, 0);
	
	if ((status & PH_ERR_MASK) == PH_ERR_SUCCESS){
		DEBUG_PRINTF ("\n\nphalMful_Sw_AuthenticateAES OK!\n");
	}
	else{
		FAIL("phalMful_Sw_AuthenticateAES Key 0 Fail");
	}
#if 0
	DEBUG_PRINTF ("\n\nRead Key 1");
	status = phalMful_FastRead(palMful, 0x30, 0x33, &pBuff, &numOfBytes);
	
	/* check for Success */
	if ((status & PH_ERR_MASK) == PH_ERR_SUCCESS)
	{
		DEBUG_PRINTF ("\n\nKey 1\n");
	
		phApp_Print_Buff(pBuff, numOfBytes);
	}
	else{
		FAIL("Read Key 1 Fail");
	}
#endif 
	
	
	DEBUG_PRINTF ("\n\n");
}

